//
//  SYClassHourView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/3/27.
//  Copyright © 2020年 SanYi. All rights reserved.
//

#import "SYClassHourView.h"
#import "SYTopLabView.h"
#import "SYClassHourTableViewCell.h"
#import "SYSelectClassViewController.h"
#import "SYStudentView.h"

@interface SYClassHourView () <UITableViewDelegate,UITableViewDataSource,SYSelectClassViewControllerDelegate>
@property (strong, nonatomic) SYTopLabView *topLabView;
@property (strong, nonatomic) SYBackTopView *backView;//顶部viwe
@property (strong, nonatomic) UITableView *curTableView;//
@property (strong, nonatomic) NSMutableArray *datalistArr;
@property (strong, nonatomic) UIButton *selectBtn;
@property (strong, nonatomic) SYSelectClassViewController *selectClassVC;
@property (strong, nonatomic) SYStudentView *studentView;
@end

@implementation SYClassHourView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}

- (void)requestDataListClassId:(NSString *)classId {
    [MBProgressHUD mj_showLoading];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestClassNeedLessonListClassId:classId success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUD];
        [weakSelf.datalistArr removeAllObjects];
        [weakSelf.datalistArr addObjectsFromArray:listArr];
        [weakSelf.curTableView reloadData];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:error.domain];
        
    }];
    
    
}


- (void)initSubView {
    [self addSubview:self.backView];
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    [self addSubview:self.selectBtn];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backView.mas_centerY);
        make.right.equalTo(self.backView.mas_right).offset(-20);
        
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(40);
    }];
    
    [self addSubview:self.topLabView];
    [self.topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.backView.mas_bottom);
        make.height.mas_equalTo(45);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.topLabView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    _datalistArr = [NSMutableArray array];
    
    [_curTableView registerCellWithNIbNames:@[@"SYClassHourTableViewCell"]];
    
    
}



#pragma mark - tab
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 3;
    return self.datalistArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYClassHourTableViewCell *hourCell = [tableView dequeueReusableCellWithIdentifier:@"SYClassHourTableViewCell" forIndexPath:indexPath];
    
    NSDictionary *dict = self.datalistArr[indexPath.row];
    hourCell.kechengLab.text = [dict nilObjectForKey:@"courseName"];
    hourCell.keshiLab.text = [dict nilObjectForKey:@"lesson"];
    hourCell.keciLab.text = [dict nilObjectForKey:@"lessonTimes"];
    hourCell.shangkeLab.text = [dict nilObjectForKey:@"beginDate"];
    hourCell.quekeLab.text = [dict nilObjectForKey:@"noAttendNum"];
    hourCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return hourCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.datalistArr[indexPath.row];
    
    _studentView = [[SYStudentView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 0, CGRectGetWidth(self.frame), kScreen_HEIGHT-20)];
    [self addSubview:_studentView];
    
    [_studentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
    
    
//    __weak typeof(self) weakSelf = self;
    [NSObject executeRunloop:^{
        //        请求数据
        //        [self.hourView requestDataListClassId:@""];
        [self.studentView requestClassId:[dict nilObjectForKey:@"classId"] scheduleId:[dict nilObjectForKey:@"scheduleId"]];
    } afterDelay:kAnimate];
//    [UIView animateWithDuration:kAnimate animations:^{
//        weakSelf.studentView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), kScreen_HEIGHT-20);
//    }];
    
    
}

- (void)selectClassViewClassId:(NSString *)classId className:(nonnull NSString *)className{
//    self.classId = classId;
    [self.selectBtn setTitle:className forState:UIControlStateNormal];
    [self requestDataListClassId:classId];
    [self.selectClassVC dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -
//头部
- (SYBackTopView *)backView {
    if (!_backView) {
        _backView = [[SYBackTopView alloc] initWithTitleName:@"课时列表" baseView:self];
    }
    return _backView;
}
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] init];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.tableFooterView = [UIView new];
        
    }
    return _curTableView;
}
#pragma mark -
- (SYTopLabView *)topLabView {
    if (!_topLabView) {
        NSArray *xiaoArr = nil;
        CGFloat bili;
        
        xiaoArr = @[@"课程",@"课时",@"课次",@"上课时间",@"缺课人数"];
        bili = 0.2;
        
        
        _topLabView = [[SYTopLabView alloc] initWithDataNameArr:xiaoArr width:bili];
    }
    return _topLabView;
}
- (UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_selectBtn setTitle:@"选择班级" forState:UIControlStateNormal];
        [_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        _selectBtn.backgroundColor = [UIColor yellowColor];
        [_selectBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    }
    return _selectBtn;
}
- (void)selectBtnClick:(UIButton *)btn {
    _selectClassVC = [[SYSelectClassViewController alloc] init];
    _selectClassVC.delegate = self;
    //  弹出视图的显示样式
    self.selectClassVC.modalPresentationStyle = UIModalPresentationPopover;
    
    //  1、弹出视图的大小
    self.selectClassVC.preferredContentSize = CGSizeMake(350, 400);
    self.selectClassVC.popoverPresentationController.sourceView = btn;
    self.selectClassVC.popoverPresentationController.sourceRect = btn.bounds;
    //箭头得方向
    self.selectClassVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    self.selectClassVC.popoverPresentationController.backgroundColor = [UIColor whiteColor];
    
    __weak typeof(self) weakSelf = self;
    [self.contVC presentViewController:self.selectClassVC animated:YES completion:^{
        [weakSelf.selectClassVC requestDataList];
    }];
}
@end
